﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace BUS
{
    public class CCheck
    {

        public static bool CheckShortcutKey(string ShortcutKey)
        {
            if (ShortcutKey.Trim() == null)
                return false;
            if (ShortcutKey.Trim() == BUS.COption.EnOrDisApp)
                return false;
            if (ShortcutKey.Trim() == BUS.COption.StopRunning)
                return false;
            return DAO.CConnectXML.KiemTraThem(ShortcutKey,BUS.COption.DataPath);
        }

        public static bool CheckRuningKey(string RunningKey)
        {
            if (RunningKey == null)
                return false;

            ArrayList _KeyDown = new ArrayList();
            string[] _Keys = RunningKey.Split('>', '<');
            for (int i = 0; i < _Keys.Length; i++)
            {
                if (i % 2 == 1)
                {
                    if (_Keys[i][0] != '/')
                    {
                        string[] _temp = _Keys[i].Split('=');
                        if (_temp.Length > 1)
                        {
                            try
                            {
                                Convert.ToInt32(_temp[1].Trim());
                            }
                            catch
                            {
                                return false;
                            }
                        }
                        if (_temp[0].ToUpper() != "Delay".ToUpper())
                        {
                            if (BUS.CTransferKeysCode.SwithCode(_temp[0]) == -1)
                                return false;
                            for (int k = 0; k < _KeyDown.Count; k++)
                            {
                                if (_KeyDown[k].ToString() == _temp[0])
                                {
                                    return false;
                                }
                            }
                            _KeyDown.Add(_temp[0]);
                        }
                    }
                    else
                    {
                        if (_Keys[i].Remove(0, 1).ToUpper() != "Delay".ToUpper())
                        {
                            if (BUS.CTransferKeysCode.SwithCode(_Keys[i].Remove(0, 1))==-1)
                                return false;
                            bool _IsRemove = false;
                            for (int j = 0; j < _KeyDown.Count; j++)
                            {
                                if (_KeyDown[j].ToString() == _Keys[i].Remove(0, 1))
                                {
                                    _KeyDown.RemoveAt(j);
                                    _IsRemove = true;
                                    break;
                                }
                            }
                            if (!_IsRemove)
                                return false;
                        }
                    }
                }
                else
                {
                    if (_Keys[i] != "")
                    {
                        string[] _Key = _Keys[i].Split('(', ')');
                        for (int j = 0; j < _Key.Length; j++)
                        {
                            if (j % 2 == 1)
                            {
                                string[] _temp = _Key[j].Split('=');
                                if (_temp.Length > 1)
                                {
                                    try
                                    {
                                        Convert.ToInt32(_temp[1].Trim());
                                    }
                                    catch
                                    {
                                        return false;
                                    }
                                }
                                if (_temp[0].ToUpper() != "Delay".ToUpper())
                                {
                                    if(CTransferKeysCode.SwithCode(_temp[0])==-1)
                                        return false;
                                    for (int k = 0; k < _KeyDown.Count; k++)
                                    {
                                        if (_KeyDown[k].ToString() == _temp[0])
                                        {
                                            return false;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                foreach (char Key in _Key[j])
                                {
                                    if(CTransferKeysCode.SwithCode(Key.ToString())==-1)
                                        return false;
                                    for (int k = 0; k < _KeyDown.Count; k++)
                                    {
                                        if (_KeyDown[k].ToString() == Key.ToString())
                                        {
                                            return false;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (_KeyDown.Count > 0)
                return false;
            return true;
        }

    }
}